package com.ybfenpan.service;


import com.ybfenpan.model.*;
import com.ybfenpan.model.log.LogEle;
import com.ybfenpan.model.log.LogFutou;
import com.ybfenpan.model.log.LogMoney;
import com.ybfenpan.model.log.LogShui;
import com.ybfenpan.service.core.BaseService;
import com.ybfenpan.service.log.LogEleService;
import com.ybfenpan.service.log.LogFutouService;
import com.ybfenpan.service.log.LogMoneyService;
import com.ybfenpan.service.log.LogShuiService;
import com.ybfenpan.utils.Param;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WageCurrentService extends BaseService {

    @Resource
    private ParamWageService paramWageService;

    @Resource
    private WageDetailService wageDetailService;

    @Resource
    private WageHistoryService wageHistoryService;

    @Resource
    private DailiService dailiService;

    @Resource
    private MemberService memberService;

    @Resource
    private LogMoneyService logMoneyService;

    @Resource
    private LogShuiService logShuiService;

    @Resource
    private LogFutouService logFutouService;

    @Resource
    private LogEleService logEleService;

    /**
     * @功能 对碰和管理奖
     */
    public void savePeng() {

        /********************** 计算对碰奖 *************************/
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("where", "1=1 and zuoYuYe>0 and youYuYe>0 ");
        List<Member> list = this.memberService.queryForList("select", param);
        for (Member m : list) {

            Double zuoYu = m.getZuoYuYe(); // 左边剩余数
            Double youYu = m.getYouYuYe(); // 右边剩余数
            Double pengNum = 0d; // 对碰数
            Double wage3 = 0d;
            Daili daili = this.dailiService.getByKey(m.getDailiId());
            // 确定对碰数
            if (zuoYu > youYu) {
                pengNum = youYu;
                wage3 = youYu * daili.getDuiPeng() * 0.01;
            } else {
                pengNum = zuoYu;
                wage3 = zuoYu * daili.getDuiPeng() * 0.01;
            }
            Double zuoSheng = m.getZuoYuYe() - pengNum;
            Double youSheng = m.getYouYuYe() - pengNum;

            // 数据处理
            m.setZuoYuYe(zuoSheng);
            m.setYouYuYe(youSheng);
            this.memberService.update(m);
            this.countWage3(m, wage3, daili);

        }

    }


    public void saveFenHong() {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("where", "1=1 and accountActive='T' and recommendNum10<100 and recommendNum>=2");
        List<Member> list = this.memberService.queryForList("select", param);
        ParamWage paramWage1018 = this.paramWageService.getByKey(1018);
        ParamWage paramWage1019 = this.paramWageService.getByKey(1019);
        for (Member m : list) {
            if (m.getRecommendNum10() < paramWage1019.getValue()) {
                m.setRecommendNum10(m.getRecommendNum10() + 1);
                this.memberService.update(m);
                Daili daili = this.dailiService.getByKey(m.getDailiId());
                Double wage6 = daili.getPayNum() * paramWage1018.getValue() * 0.01;
                countWage6(m, wage6);
            }
        }
    }

    /**
     * @功能 计算工資
     */
    public void saveWageCount() {

        /*********************************** 工资汇总 ***********************************/
        Double allWage = 0d; // 总工資

        Double allWage1 = 0d; // 奖金1
        Double allWage2 = 0d; // 奖金2
        Double allWage3 = 0d; // 奖金3
        Double allWage4 = 0d; // 奖金4
        Double allWage5 = 0d; // 奖金5
        Double allWage6 = 0d; // 奖金6
        Double allWage7 = 0d; // 奖金7
        Double allWage8 = 0d; // 奖金8

        Integer allRecommendNum = 0; // 直推人数
        Integer allRecommendTotalNum = 0; // 团队直推人数
        Double allRecommendYe = 0d; // 直推业绩
        Double allRecommendTotalYe = 0d; // 直推业绩

        /********************* 持久化详细工资 *********************/
        WageDetail wageDetail = new WageDetail();
        Date date = new Date();
        long temp = date.getTime() - 24 * 60 * 60 * 1000;

        wageDetail.setCommitDate(new Date(temp));

        wageDetailService.saveOrUpdate(wageDetail);


        /********************* 循环计算工资 ************************/
        List<WageCurrent> wageList = this.queryForList("select", null);
        for (WageCurrent wage : wageList) {

            Integer memberId = wage.getMemberId();

            Double wageNum = wage.getWageNum(); // 工资

            Double wage1 = wage.getWage1(); // 奖金1
            Double wage2 = wage.getWage2(); // 奖金2
            Double wage3 = wage.getWage3(); // 奖金3
            Double wage4 = wage.getWage4(); // 奖金4
            Double wage5 = wage.getWage5(); // 奖金5
            Double wage6 = wage.getWage6(); // 奖金6
            Double wage7 = wage.getWage7(); // 奖金7
            Double wage8 = wage.getWage8(); // 奖金7

            Integer recommendNum = wage.getRecommendNum(); // 直推人数
            Integer recommendTotalNum = wage.getRecommendTotalNum(); // 团队直推人数
            Double recommendYe = wage.getRecommendYe(); // 直推业绩
            Double recommendTotalYe = wage.getRecommendTotalYe();

            // 汇總
            allWage = allWage + wageNum; // 总工資
            allWage1 = allWage1 + wage1; // 奖金1
            allWage2 = allWage2 + wage2; // 奖金2
            allWage3 = allWage3 + wage3; // 奖金3
            allWage4 = allWage4 + wage4; // 奖金4
            allWage5 = allWage5 + wage5; // 奖金5
            allWage6 = allWage6 + wage6; // 奖金6
            allWage7 = allWage7 + wage7; // 奖金7
            allWage8 = allWage8 + wage8; // 奖金7


            allRecommendNum = allRecommendNum + recommendNum; // 直推人数
            allRecommendNum = allRecommendNum + recommendTotalNum; // 直推人数
            allRecommendYe = allRecommendYe + recommendYe; // 直推业绩
            allRecommendTotalYe = allRecommendTotalYe + recommendTotalYe; // 直推业绩


            // 生成累计工资
            WageHistory history = new WageHistory();

            history.setMemberId(memberId);
            history.setWageDetailId(wageDetail.getId());
            history.setWageNum(wageNum);

            history.setWage1(wage1);
            history.setWage2(wage2);
            history.setWage3(wage3);
            history.setWage4(wage4);
            history.setWage5(wage5);
            history.setWage6(wage6);
            history.setWage7(wage7);
            history.setWage8(wage8);

            history.setRecommendNum(recommendNum);
            history.setRecommendYe(recommendYe);
            history.setRecommendTotalNum(recommendTotalNum);
            history.setRemark("");

            history.setCommitDate(new Date(temp));

            wageHistoryService.saveOrUpdate(history);

        }

        // 清空當前工资

        Map<String, Object> mapWageCurrent = new HashMap<String, Object>();

        mapWageCurrent.put("wageNum", 0);
        mapWageCurrent.put("wage1", 0);
        mapWageCurrent.put("wage2", 0);
        mapWageCurrent.put("wage3", 0);
        mapWageCurrent.put("wage4", 0);
        mapWageCurrent.put("wage5", 0);
        mapWageCurrent.put("wage6", 0);
        mapWageCurrent.put("wage7", 0);
        mapWageCurrent.put("wage8", 0);
        mapWageCurrent.put("daNum", 0);
        mapWageCurrent.put("shouNum", 0);
        mapWageCurrent.put("recommendNum", 0);
        mapWageCurrent.put("recommendTotalNum", 0);
        mapWageCurrent.put("recommendYe", 0);
        mapWageCurrent.put("recommendTotalYe", 0);

        this.update(mapWageCurrent);


        /********************* 持久化详细电子股 *********************/
        //优化后：（由请求6次数据库优化为只请求一次）
        ParamWage paramWage2000 = new ParamWage();    // 当天报单人数
        ParamWage paramWage2001 = new ParamWage();    // 当天报单业绩

        Map paraMap = new HashMap();
        paraMap.put("where", "1=1 and id in (2000,2001) ");
        List<ParamWage> paramWageList = this.paramWageService.queryForList("select", paraMap);
        for (ParamWage paramWage : paramWageList) {
            if (2000 == paramWage.getId()) {
                paramWage2000 = paramWage;
                continue;
            } else if (2001 == paramWage.getId()) {
                paramWage2001 = paramWage;
                continue;
            }

        }


        double activeNumDouble = paramWage2000.getValue();    // 当天报单人数
        double activeYeDouble = paramWage2001.getValue();// 当天报单业绩


        //清零: 当天报单人数、当天报单业绩、更新今日抢单数、出局人数
        Map<String, Object> mapParamWage = new HashMap<String, Object>();

        mapParamWage.put("value", 0);
        mapParamWage.put("where", " id =2000 or id=2001");
        paramWageService.update(mapParamWage);


        Map<String, Object> mapWageDetail = new HashMap<String, Object>();
        mapWageDetail.put("id", wageDetail.getId());
        mapWageDetail.put("wageNum", allWage);
        mapWageDetail.put("wage1", allWage1);
        mapWageDetail.put("wage2", allWage2);
        mapWageDetail.put("wage3", allWage3);
        mapWageDetail.put("wage4", allWage4);
        mapWageDetail.put("wage5", allWage5);
        mapWageDetail.put("wage6", allWage6);
        mapWageDetail.put("wage7", allWage7);
        mapWageDetail.put("wage8", allWage8);

        mapWageDetail.put("recommendNum", allRecommendNum);
        mapWageDetail.put("recommendTotalNum", allRecommendTotalNum);
        mapWageDetail.put("recommendYe", allRecommendYe);
        mapWageDetail.put("recommendTotalYe", allRecommendTotalYe);
        mapWageDetail.put("activeNum", activeNumDouble);

        mapWageDetail.put("activeYe", activeYeDouble);

        wageDetailService.update(mapWageDetail);


        Param.hui = true;
    }


    /**
     * @功能 计算分红奖
     */
    private void countWage6(Member member, Double wage6) {

        if (wage6 > 0) {
            String title = "";
            Integer state = 888;
            // 判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage6 = 0d;
                title = title + "(已停薪)";
            }
            if (member.getAccountUse().equals("F")) {
                wage6 = 0d;
                title = title + "(账户已冻结)";
            }

            Map<String, Object> mapParam = new HashMap<String, Object>();
            mapParam.put("where", "1=1 and id in (1014,1015,1016) ");
            List<ParamWage> ParamList = this.paramWageService.queryForList("select", mapParam);

            Map<Integer, ParamWage> ParamMap = new HashMap<Integer, ParamWage>();
            for (ParamWage paramWage : ParamList) {
                ParamMap.put(paramWage.getId(), paramWage);
            }
            ParamMap.get(1014);

            Double futou = wage6 * ParamMap.get(1014).getValue() * 0.01;  //复投钱包
            Double shui = wage6 * ParamMap.get(1016).getValue() * 0.01;  //平台管理费
            Double money = wage6 * ParamMap.get(1015).getValue() * 0.01;   //提现钱包

            /*************************** 工资的处理 ******************/
            Map<String, Object> paramMember = new HashMap<String, Object>();
            paramMember.put("id", member.getId());
            paramMember.put("diyUpdate", " wage6=wage6+" + wage6 + ",historyWage=HistoryWage+" + money + ",money=money+" + money + ",shui=shui+" + shui + ",futou=futou+" + futou);
            this.memberService.update(paramMember);

            // 改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<String, Object>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage6=wage6+" + wage6 + ",wageNum=WageNum+" + wage6);
            this.update(paramWageCurrent);

            member = this.memberService.getByKey(member.getId());
            Double yu = member.getMoney();
            Double yuFutou = member.getFutou();
            Double yuShui = member.getShui();

            LogShui logShui = new LogShui();
            logShui.setMemberId(member.getId());
            logShui.setMemberLoginName(member.getLoginName());
            logShui.setTitle("返回分红的10%");
            logShui.setState(22);
            logShui.setTotalNum(shui);
            logShui.setIsFa("T");
            logShui.setYu(yuShui);
            logShui.setNum(shui);
            logShui.setCommitDate(new Date());
            logShui.setFaDate(new Date());
            logShuiService.saveOrUpdate(logShui);

            LogFutou logFutou = new LogFutou();
            logFutou.setMemberId(member.getId());
            logFutou.setMemberLoginName(member.getLoginName());
            logFutou.setTitle("返回分红的30%");
            logFutou.setState(22);
            logFutou.setTotalNum(futou);
            logFutou.setIsFa("T");
            logFutou.setYu(yuFutou);
            logFutou.setNum(futou);
            logFutou.setCommitDate(new Date());
            logFutou.setFaDate(new Date());
            logFutouService.saveOrUpdate(logFutou);

            LogMoney logEle = new LogMoney();
            logEle.setMemberId(member.getId());
            logEle.setMemberLoginName(member.getLoginName());
            logEle.setTitle("返回分红的60%");
            logEle.setState(22);
            logEle.setTotalNum(money);
            logEle.setIsFa("T");
            logEle.setYu(money);
            logEle.setNum(money);
            logEle.setCommitDate(new Date());
            logEle.setFaDate(new Date());
            logMoneyService.saveOrUpdate(logEle);

        }

    }

    /**
     * @功能 计算对碰奖
     */
    private void countWage3(Member member, Double wage3, Daili daili) {

        if (wage3 > 0) {
            String title = "业绩对碰奖";
            Integer state = 888;
            // 判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage3 = 0d;
                title = title + "(已停薪)";
            }
            if (member.getAccountUse().equals("F")) {
                wage3 = 0d;
                title = title + "(账户已冻结)";
            }

            // 判断封顶
            if (member.getWage3() + wage3 >= daili.getDuiFeng()) {
                wage3 = daili.getDuiFeng() - member.getWage3();
                title = title + "(封顶)";
            }

            // 查询所有关于管理奖参数
            Map<String, Object> mapParam = new HashMap<String, Object>();
            mapParam.put("where", "1=1 and id in (1000,1001,1002) ");
            List<ParamWage> ParamList = this.paramWageService.queryForList("select", mapParam);

            Map<Integer, Double> paramMap = new HashMap<Integer, Double>();
            for (ParamWage paramWage : ParamList) {
                paramMap.put(paramWage.getId(), paramWage.getValue());
            }


            /************************ 计算管理奖 *****************/
            Integer ceng = 1;
            Integer reId = member.getRecommendId();
            Member re = this.memberService.getByKey(reId);

            Double wage4 = 0d; // 管理奖
            ParamWage paramWage1001 = this.paramWageService.getByKey(1001);
            Double averageNum = 0d;

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("where", "recommendId=" + member.getId());
            List<Member> members = this.memberService.queryForList("select", param);
            if (members.size() > 0) {
                wage4 = wage3 * paramMap.get(1000) * 0.01;
                averageNum = wage4 / members.size();
                for (Member m2 : members) {
                    this.saveWage4(m2, averageNum);
                }
            }

            while (re != null) {
                if (ceng == 1) {
                    wage4 = wage3 * paramMap.get(1001) * 0.01;
                    this.countWage4(re, wage4, member);
                } else if (ceng == 2) {
                    wage4 = wage3 * paramMap.get(1002) * 0.01;
                    this.countWage4(re, wage4, member);
                    break;

                }

                reId = re.getRecommendId();
                re = this.memberService.getByKey(reId);
                ceng = ceng + 1;
            }

            /*************************** 工资的处理 ******************/
            Map<String, Object> paramMember = new HashMap<String, Object>();
            paramMember.put("id", member.getId());
            paramMember.put("diyUpdate", " wage3=wage3+" + wage3 + ",historyWage=HistoryWage+" + wage3 + ",money=money+" + wage3);
            this.memberService.update(paramMember);

            // 改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<String, Object>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage3=wage3+" + wage3 + ",wageNum=WageNum+" + wage3);
            this.update(paramWageCurrent);

            member = this.memberService.getByKey(member.getId());
            Double yu = member.getMoney();
            // 金积分日志(奖金)
            LogMoney log = new LogMoney();
            log.setMemberId(member.getId()); // 所属投资商
            log.setMemberLoginName(member.getLoginName()); // 所属投资商
            log.setTitle(title); // 日志说明
            log.setState(state); // 类型
            log.setNum(wage3); // 数量
            log.setYu(yu);
            log.setCommitDate(new Date()); // 时间
            logMoneyService.saveOrUpdate(log);

        }

    }

    /**
     * @功能 计算 管理奖
     */
    private void countWage4(Member member, Double wage4, Member one) {

        if (wage4 > 0) {
            String title = "管理奖：(代理商姓名：" + one.getLoginName() + ")";
            Integer state = 888;

            // 判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage4 = 0d;
                title = title + "(已停薪)";
            }
            if (member.getAccountUse().equals("F")) {
                wage4 = 0d;
                title = title + "(账户已冻结)";
            }


            /*************************** 工资的处理 ******************/
            Map<String, Object> paramMember = new HashMap<String, Object>();
            paramMember.put("id", member.getId());
            paramMember.put("diyUpdate", " wage4=wage4+" + wage4 + ",historyWage=HistoryWage+" + wage4 + ",money=money+" + wage4);
            this.memberService.update(paramMember);

            // 改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<String, Object>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage4=wage4+" + wage4 + ",wageNum=WageNum+" + wage4);
            this.update(paramWageCurrent);

            member = this.memberService.getByKey(member.getId());
            Double yu = member.getMoney();
            // 金积分日志(奖金)
            LogMoney log = new LogMoney();
            log.setMemberId(member.getId()); // 所属投资商
            log.setMemberLoginName(member.getLoginName()); // 所属投资商
            log.setTitle(title); // 日志说明
            log.setState(state); // 类型
            log.setNum(wage4); // 数量
            log.setYu(yu);
            log.setCommitDate(new Date()); // 时间
            logMoneyService.saveOrUpdate(log);

        }

    }

    /**
     * @功能 计算 上一代管理奖
     */
    private void saveWage4(Member member, Double wage4) {

        if (wage4 > 0) {
            String title = "上一代管理奖";
            Integer state = 888;

            // 判断该代理商是否停薪
            if (member.getStateWage().equals("F")) {
                wage4 = 0d;
                title = title + "(已停薪)";
            }
            if (member.getAccountUse().equals("F")) {
                wage4 = 0d;
                title = title + "(账户已冻结)";
            }

            /*************************** 工资的处理 ******************/
            Map<String, Object> paramMember = new HashMap<String, Object>();
            paramMember.put("id", member.getId());
            paramMember.put("diyUpdate", " wage4=wage4+" + wage4 + ",historyWage=HistoryWage+" + wage4 + ",money=money+" + wage4);
            this.memberService.update(paramMember);

            // 改动当前工资
            Integer wageCurrentId = member.getWageCurrentId();
            Map<String, Object> paramWageCurrent = new HashMap<String, Object>();
            paramWageCurrent.put("id", wageCurrentId);
            paramWageCurrent.put("diyUpdate", " wage4=wage4+" + wage4 + ",wageNum=WageNum+" + wage4);
            this.update(paramWageCurrent);

            member = this.memberService.getByKey(member.getId());
            Double yu = member.getMoney();
            // 金积分日志(奖金)
            LogMoney log = new LogMoney();
            log.setMemberId(member.getId()); // 所属投资商
            log.setMemberLoginName(member.getLoginName()); // 所属投资商
            log.setTitle(title); // 日志说明
            log.setState(state); // 类型
            log.setNum(wage4); // 数量
            log.setYu(yu);
            log.setCommitDate(new Date()); // 时间
            logMoneyService.saveOrUpdate(log);

        }

    }

}
